﻿using IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace WMS.Controllers
{
    public class BiCustomerController : Controller
    {
        private IBiCustomerService _ibcs;
        public BiCustomerController(IBiCustomerService ibcs)
        {
            _ibcs = ibcs;
        }

        /// <summary>
        /// 主页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {

            return View();
        }
        /// <summary>
        /// 初始数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetAllAsync()
        {
            var result = await _ibcs.GetAllAsync();
            if (result != null)
            {
                return Ok(new { code = 200, data = result, msg = "查询成功" });
            }
            else
            {
                return Ok(new { code = 404, msg = "错误" });
            }
        }
        /// <summary>
        /// 货品详细信息页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Details()
        {
            return View();
        }
        /// <summary>
        /// 详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Details(int id)
        {
            var result = await _ibcs.GetByIdAsync(id);
            if (result!=null)
            {
                return Ok(new { code = 300, data = result, msg = "查询成功" });
            }
            else
            {
                return Ok(new { code = 404, msg = "错误" });
            }
        }

        /// <summary>
        /// 添加页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Create()
        {
            return View();
        }

        /// <summary>
        /// 添加提交
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Create(BiCustomer date)
        {
            var result = await _ibcs.CreateAsync(date);
            if (result != null)
            {
                return Ok(new { code = 200, data = result, msg = "添加成功" });
            }
            else
            {
                return Ok(new { code = 404, msg = "错误" });
            }
        }

        /// <summary>
        /// 修改页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Edit(int id)
        {
            return View();
        }

        /// <summary>
        /// 修改提交
        /// </summary>
        /// <param name="id"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Edit(int id, BiCustomer date)
        {
            var result = await _ibcs.UpdateAsync(date);
            if (result != null)
            {
                return Ok(new { code = 200, data = result, msg = "更新成功" });
            }
            else
            {
                return Ok(new { code = 404, msg = "错误" });
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            var result = await _ibcs.DeleteAsync(id);
            if (result != null) { 
            return Ok(new { code = 200, data = result, msg = "删除成功" });
            }
            else
            {
                return Ok(new { code = 404, msg = "错误" });
            }
        }
    }
}
